package com.nativetalk.service.three.book.insert;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.book.TdNtBookCourse;
import com.nativetalk.bean.book.TdNtBookCourseWeek;
import com.nativetalk.bean.course.TwoTsNtCourseInfoDetail;
import com.nativetalk.bean.member.setmeal.TdNtMembSetmealDetail;
import com.nativetalk.bean.member.setmeal.TdNtMembSetmealState;
import com.nativetalk.bean.teacher.TdNtTeacherGetui;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import com.nativetalk.bean.teacher.TdNtTeacherTuisong;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.getui.AndroidBookTeacher;
import com.nativetalk.common.getui.IosBookTeacher;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.common.util.SendMailUtil;
import com.nativetalk.dao.TdNtBookCourseDao;
import com.nativetalk.dao.TdNtMembSetmealDao;
import com.nativetalk.dao.TdNtTeacherDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 16/7/29.
 */
@Service
public class ThreeBookInsertServiceImpl implements ThreeBookInsertService {
    private static Logger logger = Logger.getLogger(ThreeBookInsertServiceImpl.class);
    @Resource
    private TdNtBookCourseDao tdNtBookCourseDao;
    @Resource
    private TdNtMembSetmealDao tdNtMembSetmealDao;
    @Resource
    private TdNtTeacherDao tdNtTeacherDao;
    @Override
    public synchronized RetInfo addBook(TdNtBookCourse tdNtBookCourse) {
        String logInfo = this.getClass().getName() + ":addBook:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("memb_id", tdNtBookCourse.getMemb_id());
            Map<String , TwoTsNtCourseInfoDetail> tsNtCourseInfoDetailMap = (Map<String , TwoTsNtCourseInfoDetail> ) MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMapTwo);
            TwoTsNtCourseInfoDetail  tsNtCourseInfoDetail = tsNtCourseInfoDetailMap.get(tdNtBookCourse.getCou_id() + "");
            map.put("cou_id", tsNtCourseInfoDetail.getCate_id());
            TdNtMembSetmealDetail tdNtMembSetmealDetail = tdNtMembSetmealDao.findTdNtMembSetmealDetail(map);
            if (tdNtMembSetmealDetail != null) {
                int size = tdNtMembSetmealDetail.getSurplus_time()/60;
                if (size < tdNtBookCourse.getFrequency()){
                    retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                    retInfo.setTip("您的频率不能超过" + size +  "次");
                    return retInfo;
                }
                /**
                 * 添加预约课程
                 */
                tdNtBookCourse.setInsert_time(new Timestamp(System.currentTimeMillis()));
                tdNtBookCourse.setState(1);
                tdNtBookCourse.setMemb_set_id(tdNtMembSetmealDetail.getMemb_set_id());
                tdNtBookCourseDao.insertBook(tdNtBookCourse);
                /**
                 * 添加预约课程时间信息
                 */
                 if (tdNtBookCourse.getMonday().length > 0) {
                     for (int i:tdNtBookCourse.getMonday()){
                         TdNtBookCourseWeek tdNtBookCourseWeek = new TdNtBookCourseWeek();
                         tdNtBookCourseWeek.setBook_cou_id(tdNtBookCourse.getBook_cou_id());
                         tdNtBookCourseWeek.setWeek(1);
                         tdNtBookCourseWeek.setWeek_free_time(i);
                         tdNtBookCourseWeek.setDay_class_time(60);
                         tdNtBookCourseDao.insertBookWeek(tdNtBookCourseWeek);
                     }
                 }
                if (tdNtBookCourse.getSaturday().length > 0) {
                    for (int i:tdNtBookCourse.getSaturday()){
                        TdNtBookCourseWeek tdNtBookCourseWeek = new TdNtBookCourseWeek();
                        tdNtBookCourseWeek.setBook_cou_id(tdNtBookCourse.getBook_cou_id());
                        tdNtBookCourseWeek.setWeek(6);
                        tdNtBookCourseWeek.setWeek_free_time(i);tdNtBookCourseWeek.setDay_class_time(60);
                        tdNtBookCourseDao.insertBookWeek(tdNtBookCourseWeek);
                    }
                }
                if (tdNtBookCourse.getFriday().length > 0) {
                    for (int i:tdNtBookCourse.getFriday()){
                        TdNtBookCourseWeek tdNtBookCourseWeek = new TdNtBookCourseWeek();
                        tdNtBookCourseWeek.setBook_cou_id(tdNtBookCourse.getBook_cou_id());
                        tdNtBookCourseWeek.setWeek(5);
                        tdNtBookCourseWeek.setWeek_free_time(i);
                        tdNtBookCourseWeek.setDay_class_time(60);
                        tdNtBookCourseDao.insertBookWeek(tdNtBookCourseWeek);
                    }
                }
                if (tdNtBookCourse.getSunday().length > 0) {
                    for (int i:tdNtBookCourse.getSunday()){
                        TdNtBookCourseWeek tdNtBookCourseWeek = new TdNtBookCourseWeek();
                        tdNtBookCourseWeek.setBook_cou_id(tdNtBookCourse.getBook_cou_id());
                        tdNtBookCourseWeek.setWeek(7);
                        tdNtBookCourseWeek.setWeek_free_time(i);
                        tdNtBookCourseWeek.setDay_class_time(60);
                        tdNtBookCourseDao.insertBookWeek(tdNtBookCourseWeek);
                    }
                }
                if (tdNtBookCourse.getThursday().length > 0) {
                    for (int i:tdNtBookCourse.getThursday()){
                        TdNtBookCourseWeek tdNtBookCourseWeek = new TdNtBookCourseWeek();
                        tdNtBookCourseWeek.setBook_cou_id(tdNtBookCourse.getBook_cou_id());
                        tdNtBookCourseWeek.setWeek(4);
                        tdNtBookCourseWeek.setWeek_free_time(i);
                        tdNtBookCourseWeek.setDay_class_time(60);
                        tdNtBookCourseDao.insertBookWeek(tdNtBookCourseWeek);
                    }
                }
                if (tdNtBookCourse.getTuesday().length > 0) {
                    for (int i:tdNtBookCourse.getTuesday()){
                        TdNtBookCourseWeek tdNtBookCourseWeek = new TdNtBookCourseWeek();
                        tdNtBookCourseWeek.setBook_cou_id(tdNtBookCourse.getBook_cou_id());
                        tdNtBookCourseWeek.setWeek(2);
                        tdNtBookCourseWeek.setWeek_free_time(i);
                        tdNtBookCourseWeek.setDay_class_time(60);
                        tdNtBookCourseDao.insertBookWeek(tdNtBookCourseWeek);
                    }
                }
                if (tdNtBookCourse.getWednesday().length > 0) {
                    for (int i:tdNtBookCourse.getWednesday()){
                        TdNtBookCourseWeek tdNtBookCourseWeek = new TdNtBookCourseWeek();
                        tdNtBookCourseWeek.setBook_cou_id(tdNtBookCourse.getBook_cou_id());
                        tdNtBookCourseWeek.setWeek(3);
                        tdNtBookCourseWeek.setWeek_free_time(i);
                        tdNtBookCourseWeek.setDay_class_time(60);
                        tdNtBookCourseDao.insertBookWeek(tdNtBookCourseWeek);
                    }
                }
                /**
                 *更新套餐信息
                 */
                TdNtMembSetmealState tdNtMembSetmealState = new TdNtMembSetmealState();
                tdNtMembSetmealState.setMemb_set_id(tdNtMembSetmealDetail.getMemb_set_id());
                tdNtMembSetmealState.setState(3);
                tdNtMembSetmealDao.udpateTdNtMembSetmealState(tdNtMembSetmealState);
                /**
                 * todo 给老师发推送 邮件(需判断是单个老师还是所有老师)
                 */

                Map<String, Object> params = new HashMap<>();
                params.put("cou_id", tsNtCourseInfoDetail.getCate_id());
                params.put("currentPage", 0);
                params.put("pageSize", 100);
                List<TdNtTeacherGetui> tdNtTeacherGetuis = tdNtTeacherDao.findTdNtTeacherByCouId(params);
                if (tdNtTeacherGetuis != null && tdNtTeacherGetuis.size() > 0) {
                    for (int i = 0 ; i < tdNtTeacherGetuis.size() ; i ++){
                        TdNtTeacherTuisong tdNtTeacherTuisong = tdNtTeacherDao.findTdNtTeacherTuisong(tdNtTeacherGetuis.get(i).getTea_id());

                        TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(tdNtTeacherGetuis.get(i).getToken());
                        if (tdNtTeacherResult != null && tdNtTeacherTuisong.getEqu_client_id() != null && !(tdNtTeacherTuisong.getEqu_client_id().equals(Constants.default_equ_id))) {
                            /**
                             * 发推送
                             */
                            if (tdNtTeacherTuisong.getEqu_type().equals("1")) {
                                AndroidBookTeacher.apnpush("NativeTalk","Dear NativeTalk tutor, new assignment class, please check and arrange some time for the class.\n ",
                                        tdNtTeacherTuisong.getEqu_client_id(),tdNtBookCourse.getBook_cou_id());
                            } else {
                                logger.info(tdNtTeacherTuisong.getEqu_client_id());
                                IosBookTeacher.apnpush("NativeTalk","Dear NativeTalk tutor, new assignment class, please check and arrange some time for the class.\n ",
                                        tdNtTeacherTuisong.getEqu_client_id(),tdNtBookCourse.getBook_cou_id());

                            }
                            /**
                             * 发邮件
                             */
                            SendMailUtil.sendMail(tdNtTeacherTuisong.getTea_email(), "[NativeTalk]", "Dear NativeTalk tutor, new assignment class, please check and arrange some time for the class.\n");

                        }
                    }
                }

                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("您还没有购买该课程,请先购买该课程");
            }


        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
